<?php
// +----------------------------------------------------------------------
// |     【Alibaba】Model模型类
// +----------------------------------------------------------------------

declare (strict_types = 1);

namespace app\common\model;
include_once (__DIR__.'/../../../extend/Aliopen.php');

use think\Model;
use think\model\concern\SoftDelete;
use app\common\model\ClothMain as M;
use app\common\model\SystemConfig as CONFIG;

class Alibaba extends Model{
    use SoftDelete;
	protected $name = 'cloth_main';
    protected $deleteTime = "delete_time";
	
   /*——————————————————————————————————————
    *			阿里巴巴产品
    ——————————————————————————————————————*/
   
   // 关联阿里巴巴产品id到商品表
   public static function relation_alibaba($now_page=1,$page_size=20){
		$obj=new \Aliopen();
		$ali=$obj->product_list($now_page,$page_size);
		$page_count=ceil($ali['totalRecords']/$ali['sizePerPage']);	//总页码
		$rela=[];			//存放关联的ID号
		if(count($ali['resultList'])>0){
			foreach($ali['resultList'] as $v){
				if(isset($v['productCargoNumber'])){
					$rela[]=[
						'number'=>$v['productCargoNumber'],
						'data'=>self::ali_cloth_core_pull($v,2),
					];
				}				
			}
			unset($ali['resultList']);
			
			foreach($rela as $v){
				M::where('number',$v['number'])->save($v['data']);
			}
			if($now_page>=$page_count){
				return 0;
			}else{
				return $now_page+1;	
			}
		}else{
			return 0;
		}
   }
   
   //拉回的数据提取核心参数
   public static function ali_cloth_core_pull($res,$type=1){
   	
		// type = 1 则用于抓取数据，type=2 则用于修改数据用		
		$cover=[];
		$image=$res['image']['images'];
		$obj=new \Aliopen();
		foreach($image as $v){
			$cover[]=$obj->imageHost.$v;
		}

		$data=[
			'alibaba_id'=>$res['productID'],
			'subject'=>$res['subject'],
			'content'=>$res['description'],
			'cover'=>$cover,
		];

		if($data['content']!=''){
			//删除部分不需要在小程序显示的图片
			preg_match_all("/(href|src)=([\"|']?)([^\"'>]+.(jpg|JPG|jpeg|JPEG|gif|GIF|png|PNG))/i",$data['content'],$out,PREG_PATTERN_ORDER);
			$delte_img=[
				'https://cbu01.alicdn.com/img/ibank/O1CN01NpdPdl25kp71jnPj2_!!3999917565-0-cib.jpg',
				'https://cbu01.alicdn.com/img/ibank/O1CN01zKaELq25kp6ypSyQY_!!3999917565-0-cib.jpg',
				'https://cbu01.alicdn.com/img/ibank/O1CN01vVH5eL25kp738D9M3_!!3999917565-0-cib.jpg',		
				'https://cbu01.alicdn.com/img/ibank/O1CN01TIUdoG25kp71jn95m_!!3999917565-0-cib.jpg',
				'https://cbu01.alicdn.com/img/ibank/O1CN01i1SG3e25kp6w4aozy_!!3999917565-0-cib.jpg',
				'https://cbu01.alicdn.com/img/ibank/O1CN01hYllZk25kp7252vme_!!3999917565-0-cib.jpg',
				'https://cbu01.alicdn.com/img/ibank/O1CN014gce3B25kp738CoXt_!!3999917565-0-cib.jpg',
				'https://cbu01.alicdn.com/img/ibank/O1CN016NyRMd25kp49nsZFS_!!3999917565-0-cib.jpg',
				'https://cbu01.alicdn.com/img/ibank/O1CN01MmQC5H25kp7147Xj8_!!3999917565-0-cib.jpg',
				'https://cbu01.alicdn.com/img/ibank/O1CN01cJQifq25kp6ypRdFD_!!3999917565-0-cib.jpg',
				'https://cbu01.alicdn.com/img/ibank/O1CN01Z0a0s325kp6w4axKI_!!3999917565-0-cib.jpg',
				'https://cbu01.alicdn.com/img/ibank/O1CN01eVl1Em25kp6zrrrfE_!!3999917565-0-cib.jpg',
			];
			$out=$out[3];
			$out=array_diff($out,$delte_img);
			
			$data['content']='';
			foreach ($out as $src) {
				$data['content'].='<img src="'.$src.'"><br/>';
			}
		}

		//用于写入数据库的格式
		if($type==2){
			$data['alibaba_id']=$res['productID'];
			$data['thumb']=$cover[0];
			$data['cover']=serialize($cover);
		};

		return $data;
   }
   
   // 更新阿里巴巴价格子函数
   public static function ali_price_sub($id){
		$T=M::info($id,true);
		// if($T['number']!="F2102") return true;
		$obj=new \Aliopen();
		$params=$obj->get_product($T['alibaba_id']);
		if(isset($params['errMsg'])){
			return false;
		}else{
			$params['skuInfos'][0]['price']=$T['l_price4'];
			$params['skuInfos'][1]['price']=$T['l_price1'];
			$params['saleInfo'][0]['priceRanges']['price']=$T['l_price4'];
			$ali=$obj->product_up($T['alibaba_id'],$params);
			if(isset($ali['productID'])){		//修改成功
				return true;
			}else{
				return false;
			}
		}
   }
   
   // 格式化添加阿里巴巴产品参数
   public static function ali_params($id,$params=[]){
		$attr=CONFIG::get('ali_attributes.knitting');
		$T=M::info($id);
		$extra=[
			'category_id' => 1036950,
			'category_name' => '汗布',
			'subject'=>$T['title'],
			'purpose'=>'T恤,休闲服,家居服,时装,睡衣、内衣',
			'season'=>'春,夏,秋',
		];
		//赋值产品属性
		$attr[2]['value']=$T['component'];
		$attr[4]['value']=$T['square'];
		$attr[5]['value']=$T['breadth'].'cm';
		$attr[8]['value']=$T['number'];
		$attr[9]['value']=$T['type'];
		$attr[14]['value']=$extra['category_name'];
		$extra['purpose']=explode(',',$extra['purpose']);
		$extra['season']=explode(',',$extra['season']);

		foreach($extra['purpose'] as $p){
			$attr[]=[
				'attributeID' => 100050575,
				'isCustom' => false,
				'attributeName' => '具体用途',
				'value' => $p,
			];
		}
		foreach($extra['season'] as $p){
			$attr[]=[
				'attributeID' => 30896,
				'isCustom' => false,
				'attributeName' => '适用季节',
				'value' => $p,
			];
		}
		$new_data=[
			'productCargoNumber'=>$T['number'],
			'categoryName'=>$extra['category_name'],
			'attributes'=>$attr,
			'categoryID'=>$extra['category_id'],
			'subject'=>$extra['subject'],
			'groupID'=>[],
		];
		$amountOnSale=rand(10000,99999);
		$params['skuInfos'][0]['price']=$T['l_price4'];
		$params['skuInfos'][0]['amountOnSale']=$amountOnSale;
		$params['skuInfos'][1]['price']=$T['l_price1'];
		$params['skuInfos'][1]['amountOnSale']=$amountOnSale;
		$params['saleInfo'][0]['priceRanges']['price']=$T['l_price4'];
		$params['saleInfo']['amountOnSale']=$amountOnSale*2;
		$params=array_merge($params,$new_data);

		$top_img=[
			'https://cbu01.alicdn.com/img/ibank/2019/185/980/10323089581_1975536495.jpg',
			'https://cbu01.alicdn.com/img/ibank/2019/624/101/10323101426_1975536495.jpg',
			'https://cbu01.alicdn.com/img/ibank/2019/418/470/10323074814_1975536495.jpg',	
		];
		$buttom_img=[
			'https://cbu01.alicdn.com/img/ibank/2019/308/770/10323077803_1975536495.jpg',
			'https://cbu01.alicdn.com/img/ibank/2019/656/989/10270989656_1975536495.jpg',
			'https://cbu01.alicdn.com/img/ibank/2019/328/170/10323071823_1975536495.jpg',
			'https://cbu01.alicdn.com/img/ibank/2019/911/221/10323122119_1975536495.jpg',
			'https://cbu01.alicdn.com/img/ibank/2019/540/521/10323125045_1975536495.jpg',
			'https://cbu01.alicdn.com/img/ibank/2019/278/927/10297729872_1975536495.jpg',
			'https://cbu01.alicdn.com/img/ibank/2019/079/260/10323062970_1975536495.jpg',
			'https://cbu01.alicdn.com/img/ibank/2019/595/700/10271007595_1975536495.jpg',
			'https://cbu01.alicdn.com/img/ibank/2019/417/389/10270983714_1975536495.jpg',
			'https://cbu01.alicdn.com/img/ibank/2019/278/560/10323065872_1975536495.jpg',
		];

		return $params;
   }
	
}
